﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace STRM_Test
{
    class SrtmReader
    {
        private int intLeft = 0;
        private int intRight = 0;
        private int intTop = 0;
        private int intBottom = 0;
        private int intHscale = 0;
        private int intVscale = 0;


        public WriteableBitmap OutImg;



        public SrtmReader(string filePath, Color[] ColorMap)
        {
            Int16[] header = new Int16[12];

            var outputFile = new FileStream(filePath, FileMode.OpenOrCreate);
            var breader = new BinaryReader(outputFile);
            intLeft = breader.ReadInt32();
            intRight = breader.ReadInt32();
            intTop = breader.ReadInt32();
            intBottom = breader.ReadInt32();
            intHscale = breader.ReadInt32();
            intVscale = breader.ReadInt32();

            byte[] outBuff = new byte[intHscale * intVscale * 4];

            int i = 0;
            for (int x = 0; x < intHscale; x++ )
            {
                for (int y = 0; y < intVscale; y++)
                {
                    int PixVal = 16383 - (int)(Math.Log10(breader.ReadInt16() + 10) * 1200);

                    if (PixVal > 16383) PixVal = 16383;
                    if (PixVal < 0) PixVal = 0;
                    outBuff[i] = ColorMap[PixVal].R;
                    outBuff[i + 1] = ColorMap[PixVal].G;
                    outBuff[i + 2] = ColorMap[PixVal].B;
                    i += 4;
                }
                
            }

            OutImg = new WriteableBitmap(intHscale, intVscale, 96, 96, PixelFormats.Bgr32, null);

            OutImg.WritePixels(new System.Windows.Int32Rect(0, 0, intHscale, intVscale), outBuff, 4 * intHscale, 0, 0);


            breader.Close();
            outputFile.Close();



        }
    }
}
